import Vue from 'vue'
import Vuex from 'vuex'
import rq from '../../requests'
import router from '../../router'
import qs from 'qs'
import { Notify } from 'vant';
Vue.use(Vuex)

export default {
  namespaced: true,
  state: {
    // {"name": "", "sourceId": "", "id": 2, "nickName": ""}
    userGroup: [{"name": "A", "sourceId": "2", "id": 2, "nickName": ""}],
    selectedGroup: null,
    selectedGroupToken: null,
    groupMemberList: []
  },
  mutations: {
    setUserGroup(state, {userGroup}) {
      // console.log("setUserGroup")
      // console.log(userGroup)
      state.userGroup = userGroup;
    },
    setSelectedGroup(state, {newUserGroup, groupToken}) {
      console.log("m_newUserGroup: " + newUserGroup)
      console.log("m_groupToken: " + groupToken)
      state.selectedGroup = newUserGroup;
      state.selectedGroupToken = groupToken;
    },
    setGroupMemberList(state, {newGroupMemberList}) {
      console.log("enter setGroupMemberList");
      console.log(newGroupMemberList)
      state.groupMemberList = newGroupMemberList;
    }
  },
  getters: {
    // getters 或 computed 都是 只读的
    // 就是显示界面中
    // 计算 登录的状态
    isLogin: (state) => {
      return !!state.userToken // 转换位布尔值，逻辑取反运算 再取反
    },
    getGroupList: (state) => {
      return state.userGroup.map((dto) => {
        return dto.name + "(" + dto.sourceId + ")";
      });
    },
    getGroupMemberList: (state) => {
      return state.groupMemberList
    },
    getSelectedGroup: (state, getters) => {
      // console.log("getFirstGroup")
      let dto = state.selectedGroup;
      if(dto == null) {
        return null;
      }
      return dto.name + "(" + dto.sourceId + ")";
    }
  },
  actions: {
    createGroup({commit, state, rootState, dispatch}, {groupName}) {
      let body = {
        "group": groupName,
        "userToken": rootState.user.userToken
      };
      rq.requests.post("/fic/user/group/create", qs.stringify(body))
      .then(({eCode, res, eMessage}) => {
        if(!eCode) {
          dispatch("listGroup");
          Notify({ type: 'info', message: "添加成功"});
          return
        }
        Notify({ type: 'danger', message: eMessage});
      });
    },
    listGroup({commit, state, rootState, dispatch}) {
      console.log("listGroup");
      console.log("userToken: " + rootState.user.userToken);
      let body = {
        "userToken": rootState.user.userToken
      };
      rq.requests.post("/fic/user/group/list", qs.stringify(body))
      .then(({eCode, res, eMessage}) => {
        console.log(res)
        if(!eCode) {
          let userGroup = res
          commit("setUserGroup", {userGroup: userGroup})
          if(userGroup != null && userGroup.length > 0) {
            dispatch("loadSelectedGroup", {selectedIndex: 0})
          }
          return
        }
        Notify({ type: 'danger', message: eMessage});
      });
    },
    listGroupMember({commit, state}) {
      let groupToken = state.selectedGroupToken;
      let body = {
        "groupToken": groupToken,
      };
      rq.requests.post("/fic/user/group/member/list", qs.stringify(body))
      .then(({eCode, res, eMessage}) => {
        console.log(res);
        if(!eCode) {
          let groupMemberList = res || []
          commit("setGroupMemberList", {
            newGroupMemberList: groupMemberList
          })
          return
        }
        Notify({ type: 'danger', message: eMessage});
      });
    },
    loadSelectedGroup({commit, state, rootState, dispatch}, {selectedIndex}) {
      let selectedGroup = state.userGroup[selectedIndex]
      if(selectedGroup == null) {
        return
      }
      let groupSourceId = selectedGroup.sourceId;
      let body = {
        "userToken": rootState.user.userToken,
        "sourceId": groupSourceId
      };
      rq.requests.post("/fic/user/group/token/make", qs.stringify(body))
      .then(({eCode, res, eMessage}) => {
        console.log(res)
        if(!eCode) {
          let groupToken = res && res.groupToken
          commit("setSelectedGroup", {
            newUserGroup: selectedGroup,
            groupToken: groupToken
          });
          dispatch("listGroupMember");
          dispatch("image/listImage", {}, {root: true});
          return
        }
        Notify({ type: 'danger', message: eMessage});
      });
    },
    genInviteTicket({commit, state, rootState, dispatch}, {callback}) {
      let selectedGroup = state.selectedGroup;
      let groupSourceId = selectedGroup.sourceId;
      let body = {
        "userToken": rootState.user.userToken,
        "sourceId": groupSourceId
      };
      rq.requests.post("/fic/user/group/invite_ticket/generate", qs.stringify(body))
      .then(({eCode, res, eMessage}) => {
        console.log(res)
        if(!eCode) {
          let ticket = res.ticket;
          callback(ticket);
        }
        Notify({ type: 'danger', message: eMessage});
      });
    },
    joinInviteTicket({commit, state, rootState, dispatch}, {groupTicket}) {
      let body = {
        "userToken": rootState.user.userToken,
        "groupTicket": groupTicket
      };
      rq.requests.post("/fic/user/group/invite_ticket/join", qs.stringify(body))
      .then(({eCode, res, eMessage}) => {
        console.log(res)
        if(!eCode) {
          dispatch("listGroup");
          Notify({ type: 'info', message: "加入成功"});  
        }
        Notify({ type: 'danger', message: eMessage});
      });
    }
  }
}


